[C++]看看哪里错了

来源:百度知道 编辑:UC知道 时间:2024/05/02 21:16:47
N个人围成一圈顺序编号,从1号开始按1、2、3顺序报数,报3者退出圈外,其余的人再从1、2、3开始报数,报3的人再退出圈外,依次类推。请按退出顺序输出每个退出人的原序号。要求使用环行链表编程。

#include <iostream.h>
#include <string.h>
struct student
{

int no;
struct student* next;

};//student

void main()
{

cout<<"please input N:"<<endl;
int n;
cin>>n;
int i;

student*pt,*p,*head,*pl,*temp;
head=new student;
head->no=0;
head->next=NULL;
p=head;
for(i=1;i<=n;i++)
{
pt=new student;
pt->no=i;
pt->next=NULL;
p->next=pt;
p=pt;

}//for
p->next=head->next;

p=head->next;
pl=new student;
pl=head;

while(p!=pl)
{
for(i=1;i<=2;i++)
{

p=p->next;
pl->next;

}
pl->next=p->next;
temp=p;

运行过了没问题了。
// 8.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream.h>
#include <string.h>
struct student
{
int no;
struct student* next;
};//student

void main()
{
cout<<"please input N:"<<endl;
int n;
cin>>n;
int i;
struct student *pt,*p,*head,*pl,*temp; //
head=new student;
head->no=0;
head->next=NULL;
p=head;
for(i=1;i<=n;i++)
{
pt=new student;
pt->no=i;
pt->next=NULL;
p->next=pt;
p=pt;
}
p->next=head; //
// p=head->next; //
// pl=new student; //
pl=head;
while(p!=pl)
{
for(i=0;i<2;i++)
{
p=pl;
pl=pl->next;
}
p->next=pl->next;
cout<<pl->no<<